package com.lgr.store.controller;
import com.lgr.store.service.OrderService;
import com.lgr.store.service.StorageReportService;
import com.lgr.store.utils.ExportToExcel;
import com.lgr.store.utils.PageObject;
import com.lgr.store.vo.MaterialReportVo;
import com.lgr.store.vo.OrdersVo;
import com.lgr.store.vo.StorageVo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;

@Controller
@RequestMapping("/admin/report")
public class StorageReportController {
    @Resource
	StorageReportService reportService;
    @Resource
	OrderService orderService;
    @Resource
    HttpSession session;

	@Value("${upload.absPath}")
	String absPath;
    private void initData(ModelMap map){
    	List<StorageVo> storageList = orderService.findAllStorage();
        map.addAttribute("storageList",storageList);
    }
    @RequestMapping(value="/init")
    public String init(ModelMap map){
    	initData(map);
    	return "reportInput";
    }
    @RequestMapping(value="/init2")
    public String init2(ModelMap map){
    	initData(map);
    	return "reportStat";
    }

    @RequestMapping(value="/listDetail")
    public String listDetail(HttpServletRequest request, HttpServletResponse response, OrdersVo ord, PageObject pager, ModelMap map){
        String sdate1=ord.getOrderDate();
        String sdate2=ord.getOrderDate2();
        if(ord.getOrderDate()!=null && ord.getOrderDate()!="" && ord.getOrderDate2()!=null && ord.getOrderDate2()!="" && ord.getOrderDate().length()<=10){
        	ord.setOrderDate(ord.getOrderDate() +" 00:00:00");
        	ord.setOrderDate2(ord.getOrderDate2() +" 23:59:59");
        }
        if(ord==null || ord.getOrderDate()==null){
        	if(session.getAttribute("report")!=null){
        		ord=(OrdersVo)session.getAttribute("report");
        	}
        }else{
        	session.setAttribute("report",ord);
        }
        if(ord.getOprFlag()==0){//查询
	        List<OrdersVo> reportList = reportService.listDetail(ord,pager);
	        ord.setOrderDate(sdate1);
	        ord.setOrderDate2(sdate2);
	        map.addAttribute("ord",ord);
	        pager.setDatas(reportList);
	        map.addAttribute("pager",pager);
	        initData(map);
	        return "reportInput";
        }else{//导出到Excel
        	List<OrdersVo> reportList = reportService.listDetail(ord,null);
        	String path = request.getRealPath("/");
        	try{
        		String title="出入库明细表";
        		switch (ord.getOrderType()){
					case 1:
						title="采购入库明细报表";
						break;
					case 2:
						title="采购退货明细报表";
						break;
					case 3:
						title="销售出库明细报表";
						break;
					case 4:
						title="销售退货明细报表";
						break;
					case 5:
						title="盘点损益明细报表";
						break;
				}
	        	path = new ExportToExcel().flInput(reportList, path, absPath,2,title);
	        	File file = new File(path);   
				String filename = file.getName();   
//				String ext = filename.substring(filename.lastIndexOf(".") + 1).toUpperCase();   
				response.addHeader("Content-Disposition", "attachment;filename="+ new String(filename.getBytes()));   
				response.addHeader("Content-Length", "" + file.length());   
				OutputStream toClient = new BufferedOutputStream(response.getOutputStream());   
				response.setContentType("application/octet-stream");   
				InputStream fis = new BufferedInputStream(new FileInputStream(path));   
				byte[] buffer = new byte[1024];
		        int i = -1;
		        while ((i = fis.read(buffer)) != -1) {
		        	toClient.write(buffer, 0, i);
		        }
		        fis.close();
				toClient.write(buffer);   
				toClient.flush();   
				toClient.close();   
        	}catch(Exception e){e.printStackTrace();}
			return null;
        }
    }
	@RequestMapping(value="/listStat")
	public String listStat(HttpServletRequest request, HttpServletResponse response, OrdersVo ord, PageObject pager, ModelMap map){
		String sdate1=ord.getOrderDate();
		String sdate2=ord.getOrderDate2();
		if(ord.getOrderDate()!=null && ord.getOrderDate()!="" && ord.getOrderDate2()!=null && ord.getOrderDate2()!=""  && ord.getOrderDate().length()<=10){
			ord.setOrderDate(ord.getOrderDate() +" 00:00:00");
			ord.setOrderDate2(ord.getOrderDate2() +" 23:59:59");
		}
		if(ord==null || ord.getOrderDate()==null){
			if(session.getAttribute("report")!=null){
				ord=(OrdersVo)session.getAttribute("report");
			}
		}else{
			session.setAttribute("report",ord);
		}
		if(ord.getOprFlag()==0){
			List<MaterialReportVo> reportList = reportService.storageStat(ord,pager);
			ord.setOrderDate(sdate1);
			ord.setOrderDate2(sdate2);
			map.addAttribute("ord",ord);
			pager.setDatas(reportList);
			map.addAttribute("pager",pager);
			initData(map);
			return "reportStat";
		}else{
			List<MaterialReportVo> reportList = reportService.storageStat(ord,null);
			String path = request.getRealPath("/");
			try{
				String title="库存统计报表";
				path = new ExportToExcel().listStat(reportList, path, absPath,3,title);
				File file = new File(path);
				String filename = file.getName();
				response.addHeader("Content-Disposition", "attachment;filename="+ new String(filename.getBytes()));
				response.addHeader("Content-Length", "" + file.length());
				OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
				response.setContentType("application/octet-stream");
				InputStream fis = new BufferedInputStream(new FileInputStream(path));
				byte[] buffer = new byte[1024];
				int i = -1;
				while ((i = fis.read(buffer)) != -1) {
					toClient.write(buffer, 0, i);
				}
				fis.close();
				toClient.write(buffer);
				toClient.flush();
				toClient.close();
			}catch(Exception e){e.printStackTrace();}
			return null;
		}
	}
}
